package cn.agiledata.bank.myspace.service;

import java.util.Date;

import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.dao.BankTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.PeerBankAccount;
import cn.agiledata.bank.myspace.bean.PeerBankAccountBean;
import cn.agiledata.bank.myspace.bean.PeerBankAccountQueryBean;
import cn.agiledata.bank.myspace.dao.PeerBankAccountDao;
import cn.agiledata.bank.myspace.dao.PeerBankAccountPageList;
import cn.agiledata.bank.myspace.exception.MySpaceException;

public class PeerBankAccountServiceImpl implements PeerBankAccountService {
	private PeerBankAccountDao peerBankAccountDao;
	private AccountTemplateInter accountTemplate;
	private BankTemplate bankTemplate;

	public void setPeerBankAccountDao(PeerBankAccountDao peerBankAccountDao) {
		this.peerBankAccountDao = peerBankAccountDao;
	}
	
	public void setAccountTemplate(AccountTemplateInter accountTemplate) {
		this.accountTemplate = accountTemplate;
	}
	
	public void setBankTemplate(BankTemplate bankTemplate) {
		this.bankTemplate = bankTemplate;
	}



	/**
	 * 得到银行账户列表
	 * @param bean
	 * @return
	 */
	public PageBean getPeerBankAccountList(PeerBankAccountQueryBean bean) {
		PeerBankAccountPageList pageList = new PeerBankAccountPageList(bean);
		return pageList.getAccountListPageBean();
	}


	/**
	 * 根据联系人id得到银行联系人
	 * @param peerAccountId
	 * @return
	 */
	public PeerBankAccount getPeerBankAccount(long peerAccountId) {
		// TODO Auto-generated method stub
		return peerBankAccountDao.getPeerBankAccount(new Long(peerAccountId));
	}


	/**
	 * 新增银行联系人
	 * @param bean
	 * @throws MySpaceException
	 */
	public void peerBankAccountAdd(PeerBankAccountBean bean) throws MySpaceException {
		// TODO Auto-generated method stub
		if (!isNewPeerBankAccount(String.valueOf(bean.getAccountId()), bean.getCardNo())) {
			throw MySpaceException.NOT_UNIQUE_PEER_INNER_ACCOUNT;
		}
		
		PeerBankAccount p = new PeerBankAccount();
		p = parsePeerBankAccount(p, bean);
		peerBankAccountDao.saveOrUpdatePeerBankAccount(p);
	}


	/**
	 * 修改银行联系人
	 * @param bean
	 * @throws MySpaceException
	 */
	public void peerBankAccountModify(PeerBankAccountBean bean) throws MySpaceException {
		// TODO Auto-generated method stub
		PeerBankAccount p = peerBankAccountDao.getPeerBankAccount(new Long(bean.getModifyId()));
		
		p.setBank(bankTemplate.getBankInfo(bean.getBankSeq()));
		p.setProvince(bankTemplate.getCityInfo(bean.getProvSeq()));
		p.setCity(bankTemplate.getCityInfo(bean.getCitySeq()));
		p.setBankName(bean.getBankName());
		p.setPeerName(bean.getPeerName());
		p.setPeerEmail(bean.getPeerEmail());
		p.setPeerPhone(bean.getPeerPhone());
		p.setPeerMobile(bean.getPeerMobile());
		p.setRemark(bean.getRemark());
		
		peerBankAccountDao.saveOrUpdatePeerBankAccount(p);
	}


	/**
	 * 删除银行联系人
	 * @param peerAccountId
	 */
	public void peerBankAccountDelete(String peerAccountId) {
		// TODO Auto-generated method stub
		PeerBankAccount p = peerBankAccountDao.getPeerBankAccount(new Long(peerAccountId));
		peerBankAccountDao.deletePeerBankAccount(p);
	}
	
	/**
	 * 构造银行联系人对象
	 * @param p
	 * @param form
	 * @return
	 */
	public PeerBankAccount parsePeerBankAccount(PeerBankAccount p, PeerBankAccountBean bean) {
		Account a =  accountTemplate.getAccount(String.valueOf(bean.getAccountId()));
		p.setAccount(a);	//自己的账户
		p.setBank(bankTemplate.getBankInfo(bean.getBankSeq()));
		p.setProvince(bankTemplate.getCityInfo(bean.getProvSeq()));
		p.setCity(bankTemplate.getCityInfo(bean.getCitySeq()));
		p.setCardNo(bean.getCardNo());
		p.setBankName(bean.getBankName());
		p.setPeerName(bean.getPeerName());
		p.setPeerEmail(bean.getPeerEmail());
		p.setPeerPhone(bean.getPeerPhone());
		p.setPeerMobile(bean.getPeerMobile());
		p.setSource(Constant.PEER_ACCOUNT_SOURCE_PREPARATIVE);							//账户来源：联系人
		p.setRemark(bean.getRemark());													//备注
		
		String userId = String.valueOf(a.getId());
		p.setCreateUser(userId);
		p.setUpdateUser(userId);
		
		Date now = new Date();
		p.setCreateTime(now);
		p.setUpdateTime(now);
		return p;
	}

	/**
	 * 验证是否是新的银行联系人(不重复,可添加)
	 * @param myAccountId
	 * @param cardNo
	 * @return
	 */
	public boolean isNewPeerBankAccount(String myAccountId, String cardNo) {
		// TODO Auto-generated method stub
		PeerBankAccount peerBankAccount = peerBankAccountDao.getPeerBankAccount(new Long(myAccountId), cardNo);
		if (peerBankAccount == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 新增银行联系人(在提交账户直付信息之前)
	 * @param bean
	 * @return
	 */
	public int peerBankAccountAddBeforeDrawToOther(PeerBankAccountBean bean) {
		// TODO Auto-generated method stub
		if (!isNewPeerBankAccount(String.valueOf(bean.getAccountId()), bean.getCardNo())) {
			return 2;
		}
		
		try {
			PeerBankAccount p = new PeerBankAccount();
			p = parsePeerBankAccount(p, bean);
			p.setSource(Constant.PEER_ACCOUNT_SOURCE_TRANSACTED); //账户来源：交易过账户
			peerBankAccountDao.saveOrUpdatePeerBankAccount(p);
			
			return 0;
		} catch (Exception e) {
			System.out.println("#################################################");
			System.out.println("cn.agiledata.bank.myspace.service.PeerBankAccountServiceImpl.peerBankAccountAddBeforeDrawToOther() throw exception:");
			System.out.println(e.getMessage());
			System.out.println("#################################################");
			
			return 1;
		}
	}
	
}
